package router

import (
	"ZK306/client/client"
	"ZK306/client/request"
	"ZK306/client/response"
	"ZK306/common/proto/user"
	"github.com/gin-gonic/gin"
	jwt "github.com/golangblogs/gojwt"
)

func UserLogin(c *gin.Context) {
	var data request.UserLoginRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	login, err := client.UserLogin(c, &user.UserLoginRequest{
		Mobile: data.Mobile,
		Sms:    data.Sms,
	})
	if err != nil {
		response.ResponseError(c, err.Error(), err)
		return
	}

	claims := jwt.CustomClaims{ID: uint(login.UserId)}
	token, err := jwt.NewJWT("qq").CreateToken(claims)
	if err != nil {
		response.ResponseError(c, "token加密失败", err)
		return
	}
	response.ResponseSuccess(c, "用户登录成功", token)
}

func UserSms(c *gin.Context) {
	var data request.UserSmsRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	sms, err := client.UserSms(c, &user.UserSmsRequest{Mobile: data.Mobile})
	if err != nil {
		response.ResponseError(c, err.Error(), err)
		return
	}
	response.ResponseSuccess(c, "短信发送成功", sms.Success)
}
